package com.jastar.android.game.cannon.model;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import com.jastar.android.game.richman.logic.weight.WeightSet;

import java.util.ArrayList;
import java.util.List;

public class Target extends CollidableEntity {

    private static final WeightSet<Target> set;
    private Paint paint, scorePaint;
    private int radius;
    private int score;
    private boolean isHit;

    static {
        List<Target> list = new ArrayList<>();
        list.add(new Target(8, 30, 1, 0, 0));
        list.add(new Target(4, 50, 4, 0, 0));
        list.add(new Target(2, 75, 8, 0, 0));
        list.add(new Target(1, 100, 8, 0, 0));
        set = new WeightSet<>(list);
    }

    public static Target random(float x, float y) {
        Target random = set.random();
        return new Target(random.score, random.radius, random.getWeight(), x, y);
    }

    public Target(int score, int radius, int weight, float x, float y) {
        super(x, y, weight);
        this.score = score;
        this.radius = radius;
        this.x = x;
        this.y = y;
        this.isHit = false;

        paint = new Paint();
        paint.setColor(0xFFFF6666);

        scorePaint = new Paint();
        scorePaint.setTextAlign(Paint.Align.CENTER);
        scorePaint.setTextSize(radius);
        scorePaint.setColor(Color.WHITE);
    }

    @Override
    public boolean checkCollision(float x, float y, float r) {
        if (isHit) return false;

        float dx = this.x - x;
        float dy = this.y - y;
        float distance = (float) Math.sqrt(dx * dx + dy * dy);

        if (distance < radius + r) {
            isHit = true;
            return true;
        }
        return false;
    }

    public boolean isHit() {
        return isHit;
    }

    public int getScore() {
        return score;
    }

    public int getRadius() {
        return radius;
    }

    @Override
    public void draw(Canvas canvas) {
        if (isHit) {
            // 击中效果
            Paint paint = new Paint();
            paint.setColor(Color.argb(128, 255, 0, 0));
            canvas.drawCircle(x, y, radius * 1.5f, paint);
            return;
        }

        // 正常目标绘制
        canvas.drawCircle(x, y, radius, paint);
        canvas.drawText(String.valueOf(score), x, y, scorePaint);
    }

    public void setHit(boolean hit) {
        this.isHit = hit;
    }

}